package menu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import state.Profile;
import chesskers.Driver;
import chesskers.ImagePanel;
import chesskers.UIManager;
import state.GameState;

public class LoadGameFrame extends MenuWindow{
	
	ImagePanel panel_mainmenu = new ImagePanel(new ImageIcon("img/box_background.png").getImage());
	JButton button_mm_history = new JButton("Select");
	JButton button_mm_quit = new JButton("Back");
	DefaultListModel listModel;
	JList list;
	ArrayList<String> fileNames = new ArrayList<String>();
	
	public LoadGameFrame() {
		super("Chess-kers!");
		
		button_mm_history.setBounds(150, 360, 300, 50);
		button_mm_quit.setBounds(150, 415, 300, 30);
		
		panel_mainmenu.add(button_mm_history);
		MakeButtonAction(button_mm_history, "select");
		panel_mainmenu.add(button_mm_quit);
		MakeButtonAction(button_mm_quit, "back");
		
		this.add(panel_mainmenu);
		listModel = new DefaultListModel();
		
		// get saved name
		File directory = new File(".");
		File[] files = directory.listFiles();
		
		if(Driver.state == Driver.state.LoadChess)
		{
			for(int i = 0; i < files.length; i++){
				if(files[i].getName().endsWith(".chesssave")){
					listModel.addElement(files[i].getName());
					fileNames.add(files[i].getName());
				}
			}
		}
		else
		{
			for(int i = 0; i < files.length; i++){
				if(files[i].getName().endsWith(".checksave")){
					listModel.addElement(files[i].getName());
					fileNames.add(files[i].getName());
				}
			}
		}
		
		list = new JList(listModel);
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(175, 80, 250, 280);
		panel_mainmenu.add(scrollPane);
		
		setSize(600, 500);
		setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
	}
	
	
	@Override
	public void Action(String cmd) {
		// TODO Auto-generated method stub
		switch (cmd) {
		case "select":
			int selectedIndex = list.getSelectedIndex();
			String loadFile = fileNames.get(selectedIndex);
			try{
				FileInputStream fIn = new FileInputStream(loadFile);
				if(fIn.available() != 0){
					ObjectInputStream in = new ObjectInputStream(fIn);
					Driver.gameState = (GameState) in.readObject();
					in.close();
					fIn.close();
				}
				else
				{
					fIn.close();
				}
			}
			catch(IOException e){
				JOptionPane.showMessageDialog(this, "Error loading game");
				JOptionPane.showMessageDialog(this, e.toString());
			}
			catch(ClassNotFoundException ex){
				JOptionPane.showMessageDialog(this, "Error 2 loading game");
			}
			
			if (Driver. state == Driver.state.LoadCheck){
				Driver.state = Driver.state.CheckGame;
			}	
			else if (Driver. state == Driver.state.LoadChess){
				Driver.state = Driver.state.ChessGame;
			}
			new UIManager().start();
		    dispose();
			break;			
		case "back":
			Driver.state = Driver.state.MainMenu;
			new UIManager().start();
		    dispose();
			break;		
		default:
			throw new UnsupportedOperationException("Invalid button code: "+cmd);
		}
	}	
}